import * as systemService from '../../services/system';

export default {
  namespace: 'customerType',
  state:{
    lists:[],
  },
  reducers: {
    updateLists(state, {payload}) {
      const newstate = state;
      newstate.lists = payload;
      return newstate;
    },
    updateRow(state, {payload}) {
      const newstate = state;
      let index=null;
      const datas=newstate.lists;
      for(var i=0;i<datas.length;i++){
        if(datas[i].id==payload.id){
          index=i;
          break;
        }
      }
      if(index!=null){
        newstate.lists[index]=payload;
      }
      return newstate;
    },
  },
  effects: {
    *e_getCustomerTypes({payload}, {call, put}) {
      const {data} = yield call(systemService.getCustomerTypes);
      yield put({type: 'updateLists', payload: data});
    },
    *e_deleteCustomerType({payload}, {call, put}) {
      yield call(systemService.deleteCustomerType,payload);
      yield put({type: 'e_getCustomerTypes'});
    },
    *e_add({payload}, {call, put}) {
      yield call(systemService.addOrUpdateCustomerType,payload);
      yield put({type: 'e_getCustomerTypes'});
    },
    *e_update({payload}, {call, put}) {
      const {data} = yield call(systemService.addOrUpdateCustomerType,payload);
      yield put({type: 'updateRow',payload:data});
    },
  },
}
